package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.main.MyDocument;
import com.sanqing.model.Borrow;
import com.sanqing.model.Operater;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class BackBook extends JInternalFrame {
    Borrow borrow = new Borrow();
    Operater backOperator;
    JTextField ISBN = new JTextField(14);
    JTextField readerId = new JTextField(15);

    public BackBook(Operater backOperator) {
        super("还书");
        setLayout(new BorderLayout());
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setBounds(100, 100, 700, 350); //设置窗体位置和大小
        this.backOperator = backOperator;
        JPanel borrowInfoPanel = new JPanel(new FlowLayout());

        final JLabel ISBNLabel = new JLabel("ISBN:");
        ISBNLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        borrowInfoPanel.add(ISBNLabel);

        ISBN.setFont(new Font("宋体", Font.PLAIN, 25));
        ISBN.setDocument(new MyDocument(13));
        borrowInfoPanel.add(ISBN);

        final JLabel readerIdLabel = new JLabel("借阅证号:");
        readerIdLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        borrowInfoPanel.add(readerIdLabel);

        readerId.setFont(new Font("宋体", Font.PLAIN, 25));
        borrowInfoPanel.add(readerId);

        getContentPane().add(borrowInfoPanel, BorderLayout.NORTH);

        JPanel butPanel = new JPanel();
        JButton submit = new JButton("还书");
        JButton reset = new JButton("重置");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        reset.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new submitBut());
        reset.addActionListener(new resetBut());

        butPanel.add(submit);
        butPanel.add(reset);

        getContentPane().add(butPanel, BorderLayout.SOUTH);
        validate();
    }

    private class submitBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // 两个文本框中须有内容
            if (ISBN.getText().equals("") || readerId.getText().equals("")) {
                JOptionPane.showMessageDialog(BackBook.this, "请填写信息");
                return;
            }

            //初始化模型类
            borrow.setOperatorId_back(backOperator.getId());
            borrow.setISBN(ISBN.getText());
            borrow.setBorrowDate(new Date());
            borrow.setIsBack(0);
            borrow.setReaderId(readerId.getText());

            //还书逻辑
            int a=Dao.canBackBook(borrow);
            switch (a) {
                case 0:
                    JOptionPane.showMessageDialog(BackBook.this, "无此图书或证号错误!");
                    break;
                case 1:
                    JOptionPane.showMessageDialog(BackBook.this, "无此读者或证号错误!");
                    break;
                case 2:
                    JOptionPane.showMessageDialog(BackBook.this, "您未借过此书!");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(BackBook.this, "还书成功!");
                    Dao.backBook(borrow,0);
                    break;
                case 4:
                    JOptionPane.showMessageDialog(BackBook.this, "还书失败!");
                    break;
                default:
                    JOptionPane.showMessageDialog(BackBook.this, "你还书时间已逾期，需扣除"+a+"天押金");
                    Dao.backBook(borrow,a);
                    JOptionPane.showMessageDialog(BackBook.this, "还书成功!");
                    break;
            }
        }
    }

    private class resetBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ISBN.setText("");
            readerId.setText("");
        }
    }
}
